package com.course.domain.dao;

import com.course.domain.model.LearnStat;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * Created by Justin on 2017/7/1.
 */
public interface LearnStatDao extends JpaRepository<LearnStat, Integer> {

    LearnStat findByStudentIdAndCourseIdAndWeekIdxAndWeekDay(int studentId, int courseId, int weekIdx, int weekDay);

    List<LearnStat> findByCourseIdAndWeekIdxAndWeekDay(int courseId, int weekIdx, int weekDay);

    @Modifying
    @Query("update LearnStat stat set stat.done = stat.done + 1 where stat.studentId = ?1 and stat.courseId = ?2 and stat.weekIdx = ?3 and stat.weekDay =?4 and stat.total > stat.done")
    int done(int studentId, int courseId, int weekIdx, int weekDay);
}
